﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class RandHelper
    {
        /// <summary>
        /// 获取指定长度的纯数字随机字符串
        /// </summary>
        /// <param name="len">字符串长度</param>
        /// <returns></returns>
        public static string GetRandNubmer(int len)
        {
            StringBuilder sb = new StringBuilder(len);
            Random rand = new Random();
            for (int i = 0; i < len; i++)
            {
                sb.Append(rand.Next(0, 9));
            }
            return sb.ToString();
        }
        /// <summary>
        /// 获取指定长度的纯字母的随机字符串
        /// </summary>
        /// <param name="len">长度</param>
        /// /// <param name="flag">0:大小写混合（默认） 1:小写2：大写</param>
        /// <returns></returns>
        public static string GetRandLetter(int len,int flag=0)
        {
            StringBuilder sb = new StringBuilder(len);
            Random rand = new Random();
            char x = ' ';
            for (int i = 0; i < len; i++)
            {
                switch(flag)
                {
                    case 0:
                        int y = rand.Next() % 2;
                        if (y == 0)
                        {
                            x = 'a';
                        }
                        else
                        {
                            x='A';
                        }
                        break;
                    case 1:
                        x = 'a';
                        break;
                    case 2:
                        x = 'A';
                        break;
                }
                char temp = (char)(rand.Next() % 26 + x);
                sb.Append(temp.ToString());
            }

            return sb.ToString();
        }
        /// <summary>
        /// 获取指定长的混合字符串
        /// </summary>
        /// <param name="len">长度</param>
        /// <returns></returns>
        public static string GetRandMix(int len)
        {
            StringBuilder sb = new StringBuilder(len);
            Random rand = new Random();
            for (int i = 0; i < len; i++)
            {
                int x = rand.Next() % 3;
                char temp = 'x';
                if (x == 0)
                {
                    temp =  (char)(rand.Next() % 26 + 'a');
                    
                }
                else if (x == 1)
                {
                    temp = (char)(rand.Next(0, 9)+'0');
                }
                else
                {
                    temp = (char)(rand.Next() % 26 + 'A');
                }
                sb.Append(temp.ToString());
            }

            return sb.ToString();
        }
    }
}
